package club.beimeng.sendEmail;

import com.sun.mail.util.MailSSLSocketFactory;
import org.apache.commons.beanutils.PropertyUtils;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.security.GeneralSecurityException;
import java.util.Properties;

public class EmailSender {
    private Session session; // session变量
    private MimeMessage mimeMessage; // 邮件本体


    private EmailSender( BEmail message){ // 构造方法
        Properties prop = new Properties();
        prop.setProperty("mail.host", "smtp.qq.com");  //设置QQ邮件服务器
        prop.setProperty("mail.transport.protocol", "smtp"); // 邮件发送协议

        // 关于QQ邮箱，还要设置SSL加密，加上以下代码即可
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        assert sf != null;
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);

        session = Session.getDefaultInstance(prop);

        mimeMessage = new MimeMessage(session); // 配置邮件本体
        mimeMessage = message.getMimeMessage(mimeMessage); // 配置变量
        //设置自定义发件人昵称
        String nick="";
        try {
            nick=javax.mail.internet.MimeUtility.encodeText("小艾同学");

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        try {
            mimeMessage.setFrom(new InternetAddress(nick+" <"+"2877025798@qq.com"+">"));
//            mimeMessage.setFrom("2877025798@qq.com");
//            mimeMessage.setFrom(new InternetAddress("2877025798@qq.com")); // 这个可能可以改动
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    public static EmailSender setMessage(BEmail message){ // 配置message对象。 使用的时候直接EmailSender.setMessage(one).sentTo(s);
        return new EmailSender(message);
    }

    public Boolean sentTo(String emailNumber){// // 设置发送目标邮件地址

        try {
            mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(emailNumber));
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return send();
    }

    public Boolean sentToThem(String[] emailNumbers){ // 设置发送目标邮件地址
        InternetAddress[] internetAddresses = new InternetAddress[emailNumbers.length]; // 配置新数组，并赋值
        for (int i = 0; i < emailNumbers.length; i++) {
            try {
                internetAddresses[i] = new InternetAddress(emailNumbers[i]);
            } catch (AddressException e) {
                e.printStackTrace();
            }
        }
        try {
            mimeMessage.setRecipients(Message.RecipientType.TO, internetAddresses);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return send();
    }

    private boolean send() {
        try {
            //2 通过session得到transport对象,用它装载Message(邮件本体对象)
            Transport ts = session.getTransport();

            //3、为transport对象配置发件人，授权码，以及目标服务器
            ts.connect("smtp.qq.com", "2877025798@qq.com", "ssyyzjurppykdeag");// 铭艺
//            ts.connect("smtp.163.com", "beimengclub@163.com", "VTMINOMQLJISRPDS");// 网易邮箱 // 应该自动分配发送账号
            //5、发送邮件
            ts.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
            ts.close();
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
